package com.roc.web.action.windcontroll;
/*
 * 高危返回码Action
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.TblMgmRiskCodeBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.service.ReturnCodeService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
	@Component
	@Namespace("/returnCode")
	@SuppressWarnings("serial")
	@Scope(BeanDefinition.SCOPE_PROTOTYPE)
	public class ReturnCodeAction  extends PaginationBaseAction {
		
		private Logger logger=LogManager.getLogger(ReturnCodeAction.class);
		private ReturnCodeSearchCondition searchCondition;
		
		private TblMgmRiskCodeBean  code;
		@Autowired
		IPageService pagenitionService;
		
		@Autowired
		ReturnCodeService returnCodeService;
		
		private List<TblMgmRiskCodeBean> returnCodeList = new ArrayList<TblMgmRiskCodeBean>();
        /**
         * 初始化非高危返回码
         * @Title: alloat
         * @Description: TODO
         * @param @return    
         * @return String    
         * @throws
         */
		public String alloat(){
			try {
				returnCodeList = returnCodeService.queryNoReturnExplan();
			} catch (Exception e) {
				addActionError("加载非高危返回码失败，请联系客服人员");
				logger.error(e.getMessage(),e);
			}
			return SUCCESS;
		}
		
		/**
		 * 初始化高危返回码主页面
		 * @return
		 * @throws Exception
		 */
		@Action(value = "init", results = { @Result(name = "success", location = "/pages/returnCode/queryReturnCode.jsp"),
				@Result(name = "error", location = "/error.jsp")
	    })	
		public String init() throws Exception{
			     alloat();
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			if(op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()) || op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode()))
			{
				return SUCCESS;
			}
			addActionError("无权限访问!!!");
			return ERROR;	   
	    }
	/**
	 * 高危返回码查询
	 * @Title: queryReturnCode
	 * @Description: TODO
	 * @param @return
	 * @param @throws Exception    
	 * @return String    
	 * @throws
	 */
		@Action(value = "queryReturnCode", results = { @Result(name = "success", location = "/pages/returnCode/queryReturnCode.jsp"),
				@Result(name = "error", location = "/error.jsp")
	    })
		public String queryReturnCode() throws Exception{
			alloat();
			try {
				
				if(searchCondition==null){
					searchCondition=(ReturnCodeSearchCondition)getSearchConditionFromSession();
					if(searchCondition==null){
						searchCondition=new ReturnCodeSearchCondition();
					}
				}
				int rows=pagenitionService.countSQL(searchCondition);
				if(rows>0){
					searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
					dealWithTheListSpecially(searchRsList);
					
					List<TblMgmRiskCodeBean> lists=new ArrayList<TblMgmRiskCodeBean>();
					if(searchRsList!=null&&searchRsList.size()>0)
					{
						Iterator<?> iterator=searchRsList.iterator();
						while(iterator.hasNext())
						{
							Object[] obj=(Object[]) iterator.next();
							TblMgmRiskCodeBean code=new TblMgmRiskCodeBean();
							code.setValue(Constance.Null2EmptyTrim(obj[0]));
							code.setExplan(Constance.Null2EmptyTrim(obj[1]));
							lists.add(code);
						}
						searchRsList.clear();
						searchRsList.addAll(lists);
					}
				}
				
				this.setTotalPage(this.calcuTotalPage(rows));
				getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
				getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
				getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
				setSearchToConditionSession(searchCondition);
				
			} catch (Exception e) {
				
				logger.error(e.getMessage(),e);
				addActionError(e.getMessage());
				return ERROR;
			}		
		   return SUCCESS;
	    }
		/**
		 * 初始化添加高危返回码
		 * @Title: initAdd
		 * @Description: TODO
		 * @param @return    
		 * @return String    
		 * @throws
		 */
		@Action(value = "initAdd", results = { @Result(name = "success", location = "/pages/returnCode/addReturnCode.jsp"),
				@Result(name = "error", location = "/error.jsp")
	    })
		public String initAdd()
		{
			try {
				returnCodeList = returnCodeService.queryreturnExplan();
			} catch (Exception e) {
				addActionError("加载高危返回码失败，请联系客服人员");
				logger.error(e.getMessage(),e);
			}
			return SUCCESS;		
		}
		/**
		 * 添加高危返回码操作
		 * @Title: addReturnCode
		 * @Description: TODO
		 * @param @return    
		 * @return String    
		 * @throws
		 */
		@Action(value = "addReturnCode", results = { @Result(name = "success", location = "/pages/returnCode/queryReturnCode.jsp"),
				@Result(name = "error", location = "/error.jsp")
	    })
		public String addReturnCode(){
			alloat();
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			try {
				returnCodeService.addReturnCode(searchCondition.getReturnName(), op);
				addActionMessage("添加成功!");
				try {
					queryReturnCode();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (ServiceException e) {
				e.printStackTrace();
				addActionError("添加高危返回码失败~");
			}
			return SUCCESS;		
		}
		
		/**
		 * 删除高危返回码
		 * @Title: delReturnCode
		 * @Description: TODO
		 * @param @return    
		 * @return String    
		 * @throws
		 */
		@Action(value = "delReturnCode", results = { @Result(name = "success", type="json")})
		public String delReturnCode(){
			alloat();
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			try {
				returnCodeService.deleteReturnCode(code.getValue(), op);
				addActionMessage("删除成功!");
			} catch (ServiceException e) {
				e.printStackTrace();
				addActionError("删除高危返回码失败~");
			}
			return SUCCESS;
		}

		@Override
		protected BaseSearchCondition getSearchConditionFromSubClass()
				throws Exception {

			return searchCondition;
		}

		@Override
		protected void setSearchConditionToSubClass(
				BaseSearchCondition searchCondition) throws Exception {

			this.setSearchConditionToSubClass(searchCondition);
		}

		@Override
		protected void setSearchToConditionSession(
				BaseSearchCondition searchCondition) {

			getSession().setAttribute(SEARCH_SESSION_RETURN_CODE_QUERY, searchCondition);
		}

		@Override
		protected BaseSearchCondition getSearchConditionFromSession() {

			return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_RETURN_CODE_QUERY);
		}

		@Override
		protected String getInitPageSize() {

			return Constance.PAGE_MAX_SIZE+"";
		}
		public ReturnCodeSearchCondition getSearchCondition() {
			return searchCondition;
		}
		public void setSearchCondition(ReturnCodeSearchCondition searchCondition) {
			this.searchCondition = searchCondition;
		}
		

		public TblMgmRiskCodeBean getCode() {
			return code;
		}

		public void setCode(TblMgmRiskCodeBean code) {
			this.code = code;
		}

		public List<TblMgmRiskCodeBean> getReturnCodeList() {
			return returnCodeList;
		}

		public void setReturnCodeList(List<TblMgmRiskCodeBean> returnCodeList) {
			this.returnCodeList = returnCodeList;
		}
	

	}

